package com.cyq.day19.calc;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalcSwing extends JFrame {
    public String[] bid = {"7", "8", "9", "*", "4", "5", "6", "/", "1", "2", "3", "+", "0", ".", "=", "-"};

    public CalcSwing() {
        String[] stringnum = new String[2];
        JFrame jFrame = new JFrame();
        jFrame.setTitle("计算器");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextField jTextField = new JTextField(20);
        jTextField.setHorizontalAlignment(jTextField.RIGHT);
        this.add(BorderLayout.NORTH, jTextField);
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(4, 4));
        for (int i = 0; i < bid.length; i++) {
            JButton[] jb = new JButton[bid.length];
            jb[i] = new JButton();
            jb[i].setText(bid[i]);
            jPanel.add(jb[i]);
            jFrame.pack();
            jb[i].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String show = "";
                    String num = ((JButton) e.getSource()).getText();
                    if (num.equals("*") || num.equals("/") || num.equals("+") || num.equals("-")) {
                        stringnum[0] = jTextField.getText();
                        stringnum[1] = num;
                        jTextField.setText("");
                    }
                    if (num.equals("7") || num.equals("8") || num.equals("9") || num.equals("4") || num.equals("5") || num.equals("6") || num.equals("1") || num.equals("2") || num.equals("3") || num.equals("0") ) {
                        show += num;
                        jTextField.setText(jTextField.getText() + show);
                    }
                    if (num.equals(".")){
                        if (jTextField.getText().contains(".")){
                            return;
                        }else{
                            show += num;
                            jTextField.setText(jTextField.getText() + show);
                        }
                    }
                    if (num.equals("=")) {
                        if ("+".equals(stringnum[1])) {
                            double result = Double.parseDouble(stringnum[0]) + Double.parseDouble(jTextField.getText());
                            show = result + "";
                            jTextField.setText(show);
                            stringnum[0] = show;
                        }
                        if ("-".equals(stringnum[1])) {
                            double result = Double.parseDouble(stringnum[0]) - Double.parseDouble(jTextField.getText());
                            show = result + "";
                            jTextField.setText(show);
                            stringnum[0] = show;
                        }
                        if ("*".equals(stringnum[1])) {
                            double result = Double.parseDouble(stringnum[0]) * Double.parseDouble(jTextField.getText());
                            show = result + "";
                            jTextField.setText(show);
                            stringnum[0] = show;
                        }
                        if ("/".equals(stringnum[1])) {
                            if (Double.parseDouble(stringnum[0])==0){
                                JOptionPane.showMessageDialog(null,"除数不能为0");
                                return;
                            }
                            double result = Double.parseDouble(stringnum[0]) / Double.parseDouble(jTextField.getText());
                            show = result + "";
                            jTextField.setText(show);
                            stringnum[0] = show;
                        }

                    }
                }
            });
        }
        this.add(BorderLayout.CENTER, jPanel);

    }
}